const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    isfx: app.globalData.userInfo.group_id == 2 || app.globalData.userInfo.group_id == 3,
    fxprice: 0,
    safeToBottom: util.getSafeToBottom(),
    tabactive: 0,
    list: [],
    page: 1,
    listStatus: 'loading',
    screen: util.getDateTime().format('Y-M'),
    endTime: util.getDateTime().format('Y-M'),
  },
  onLoad() {
    this.getOrders()
  },
  onReachBottom() {
    this.setData({ page: this.data.page + 1 })
    if (this.data.tabactive == 0) {
      this.getOrders()
    } else if (this.data.tabactive == 1) {
      this.getOrderListCheck()
    }
  },
  // 筛选改变
  screenChange({detail: {value}}) {
    this.setData({ screen: value })
    if (this.data.tabactive == 0) {
      this.setData({ page: 1 })
      this.getOrders()
    } else if (this.data.tabactive == 1) {
      this.setData({ page: 1 })
      this.getOrderListCheck()
    }
  },
  // 健康服务订单查询
  async getOrders() {
    const { page: p, list } = this.data
    const res = await api.getOrders({ p, month: this.data.screen })
    console.log(res);
    res.list.data = res.list.data.map(v => {
      return {
        ...v, 
        type: 'service',
        createtime: util.getDateTime(v.createtime * 1000).format('M-D h:m'),
        client: {
          ...v.client,
          age: v?.client ? new Date().getFullYear() - new Date(v.client.birthday).getFullYear() : '' 
        }
      }
    })
    this.setData({
      fxprice: res.point_price,
      list: p == 1 ? res.list.data : list.concat(res.list.data),
      listStatus: res.list.current_page < res.list.last_page ? 'loading' : res.list.current_page > res.list.last_page ? 'nodata' : res.list.current_page == res.list.last_page ? 'tobottom' : '',
    })
  },
  // 体检订单查询
  async getOrderListCheck() {
    const { page: p, list } = this.data
    const res = await api.getOrderListCheck({ p, month: this.data.screen })
    res.list.data = res.list.data.map(v => {
      return {
        ...v, 
        check_content: JSON.parse(v.check_content),
        type: 'check',
        createtime: util.getDateTime(v.createtime * 1000).format('M-D h:m'),
        client: {
          ...v.client,
          age: v?.client ? new Date().getFullYear() - new Date(v.client.birthday).getFullYear() : ''
        }
      }
    })
    this.setData({
      fxprice: res.point_price,
      list: p == 1 ? res.list.data : list.concat(res.list.data),
      listStatus: res.list.current_page < res.list.last_page ? 'loading' : res.list.current_page > res.list.last_page ? 'nodata' : res.list.current_page == res.list.last_page ? 'tobottom' : '',
    })
  },
  // 跳转订单详情
  gotodetail({ currentTarget: { dataset: { item } } }) {
    wx.navigateTo({ url: "../purchased/index?id=" + item.id + "&type=" + item.type })
  },
  // tab改变
  tabchange({ currentTarget: { dataset: { index } } }) {
    if (this.data.tabactive != index) {
      this.setData({ tabactive: index })
      if (index == 0) {
        this.setData({ page: 1 })
        this.getOrders()
      } else if (index == 1) {
        this.setData({ page: 1 })
        this.getOrderListCheck()
      }
    }
  },
})
